package com.dwsoft.word;

import com.dwsoft.pojo.DataKpiDO;
import com.dwsoft.xml.XChart;
import org.jfree.chart.*;
import org.jfree.chart.plot.DefaultDrawingSupplier;
import org.jfree.chart.plot.PieLabelLinkStyle;
import org.jfree.chart.renderer.category.StandardBarPainter;
import org.jfree.chart.renderer.xy.StandardXYBarPainter;
import org.jfree.ui.RectangleInsets;
import word.w2004.Document2004;
import word.w2004.elements.Image;

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author sqw
 * @version 1.0
 * @description TODO
 * @ClassName WChart
 * @Date 2020/8/14
 * @since jdk1.8
 */
public abstract class WChart<T> extends WElement<XChart>{

    protected static Map<String,ChartTheme> chartThemes=new HashMap<>();
    protected static Font TITLE_FONT=new Font("黑体", Font.BOLD, 20);
    protected static Font FONT = new Font("宋体", Font.PLAIN, 12);
    public static Color[] CHART_COLORS = {
            Color.red,
            Color.yellow,
            Color.blue,
            Color.green,
            new Color(153, 158, 255),
            new Color(255, 117, 153),
            new Color(253, 236, 109),
            new Color(128, 133, 232),
            new Color(158, 90, 102)
    };// 颜色

    static {
        // 设置中文主题样式 解决乱码
        StandardChartTheme chartTheme = new StandardChartTheme("CN");
        // 设置标题字体
        chartTheme.setExtraLargeFont(TITLE_FONT);
        // 设置图例的字体
        chartTheme.setRegularFont(FONT);
        // 设置轴向的字体
        chartTheme.setLargeFont(FONT);
        chartTheme.setSmallFont(FONT);
        chartTheme.setTitlePaint(new Color(51, 51, 51));
        chartTheme.setSubtitlePaint(new Color(85, 85, 85));
        chartTheme.setLegendBackgroundPaint(Color.WHITE);// 设置标注
        chartTheme.setLegendItemPaint(Color.BLACK);//
        chartTheme.setChartBackgroundPaint(Color.WHITE);
        // 绘制颜色绘制颜色.轮廓供应商
        // paintSequence,outlinePaintSequence,strokeSequence,outlineStrokeSequence,shapeSequence
        Paint[] OUTLINE_PAINT_SEQUENCE = new Paint[] { Color.WHITE };
        // 绘制器颜色源
        DefaultDrawingSupplier drawingSupplier = new DefaultDrawingSupplier(
                CHART_COLORS, CHART_COLORS, OUTLINE_PAINT_SEQUENCE,
                DefaultDrawingSupplier.DEFAULT_STROKE_SEQUENCE,
                DefaultDrawingSupplier.DEFAULT_OUTLINE_STROKE_SEQUENCE,
                DefaultDrawingSupplier.DEFAULT_SHAPE_SEQUENCE);
        chartTheme.setDrawingSupplier(drawingSupplier);
        chartTheme.setPlotBackgroundPaint(Color.WHITE);// 绘制区域
        chartTheme.setPlotOutlinePaint(Color.WHITE);// 绘制区域外边框
        chartTheme.setLabelLinkPaint(new Color(8, 55, 114));// 链接标签颜色
        chartTheme.setLabelLinkStyle(PieLabelLinkStyle.CUBIC_CURVE);
        chartTheme.setAxisOffset(new RectangleInsets(5, 12, 5, 12));
        chartTheme.setDomainGridlinePaint(new Color(192, 208, 224));// X坐标轴垂直网格颜色
        chartTheme.setRangeGridlinePaint(new Color(192, 192, 192));// Y坐标轴水平网格颜色
        chartTheme.setBaselinePaint(Color.WHITE);
        chartTheme.setCrosshairPaint(Color.BLUE);// 不确定含义
        chartTheme.setAxisLabelPaint(new Color(51, 51, 51));// 坐标轴标题文字颜色
        chartTheme.setTickLabelPaint(new Color(67, 67, 72));// 刻度数字
        chartTheme.setBarPainter(new StandardBarPainter());// 设置柱状图渲染
        chartTheme.setXYBarPainter(new StandardXYBarPainter());// XYBar 渲染
        //chartTheme.setItemLabelPaint(Color.);
        chartTheme.setThermometerPaint(Color.white);// 温度计
        chartThemes.put("standard",chartTheme);
    }

    @Override
    public void instance(XChart xmlNode, Document2004 document) throws NoSuchMethodException, IOException {
        int randomNum = (int)(Math.random()*(9999-1000+1))+1000;
        String path=sysUserHome+"/"+FILE_DIR+"/"+randomNum+".jpg";
        try {
            JFreeChart chart = initializeChart((T)xmlNode);
            ChartUtilities.saveChartAsJPEG(new File(path), chart, xmlNode.getWidth(), xmlNode.getHeight());
            word.w2004.elements.Image image= Image.from_FULL_LOCAL_PATHL(path).setHeight(xmlNode.getHeight()+"px").setWidth(xmlNode.getWidth()+"px").create();
            document.addEle(image);
        } finally {
            File file=new File(path);
            if(file.exists()) file.delete();
        }
    }

    protected abstract JFreeChart initializeChart(T xmlNode);

    protected List<Map<String,String>> data(String kpi){
        DataKpiDO kpiDO = dataKpiMapper.getKpi(kpi);
        List<Map<String,String>> rs = dataKpiMapper.getKpiValueForList(kpiDO.getKpiScript());
        return rs;
    }


}
